package com.ry.service.controller;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.ry.service.model.User;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;


@RestController
@RequestMapping("/ribbon/user")
public class RibbonController {

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping(value = "/sayHello/{id}", method = RequestMethod.GET)
    public String greeting(@PathVariable("id") Long id, @RequestParam("name") String name, @RequestParam("age") Integer age) {

        String url = "http://ServiceA/user/sayHello/" + id + "?name="+name + "&age="+age;
        String str = restTemplate.getForObject(url, String.class);
        if (StringUtils.isNotBlank(str)){
            System.out.println(str);
            return str;
        }
        return "{}";
    }



    @LoadBalanced
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

}
